﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Brio
{
    public partial class SheetMusicViewerControl : UserControl
    {
        public SheetMusicViewerControl()
        {
            InitializeComponent();
        }

        private string[] ImageLocations;
        private Bitmap[] Images;
        private int Index;

        public void ResetViewer()
        {
            SheetMusicViewer.BackgroundImage = null;

            if (Images != null)
            {
                for (int i = 0; i < Images.Length; i++)
                {
                    Images[i].Dispose();
                }
            }

            ImageLocations = null;

        }

        public void LoadDirectoryImages(string directoryPath, string imageExtension)
        {
            // Store all Image Locations
            ImageLocations = Directory.GetFiles(directoryPath, "*." + imageExtension);


            if (ImageLocations.Length != 0)
            {
                // Load all images
                Images = new Bitmap[ImageLocations.Length];

                for (int i = 0; i < Images.Length; i++)
                {
                    Images[i] = new Bitmap(ImageLocations[i]);
                }

                // Set Index to first Image
                Index = 0;

                // Update buttons
                UpdateControls();

                // Set Image
                SetViewer(Index);

                Visible = true;
            }
            else
            {
                Visible = false;
            }
        }

        public void UpdateControls()
        {
            BackButton.Visible = (Index != 0) ? true : false;
            ForwardButton.Visible =  (Index != Images.Length - 1) ? true : false;

            PageLabel.Text = (Index + 1) + " of " + Images.Length;
        }

        public void SetViewer(int index)
        {
            SheetMusicViewer.BackgroundImage = Images[index];
        }

        private void ForwardButton_Click(object sender, EventArgs e)
        {
            Index++;

            // Update buttons
            UpdateControls();

            // Set Image
            SetViewer(Index);
        }

        private void BackButton_Click(object sender, EventArgs e)
        {
            Index--;

            // Update buttons
            UpdateControls();

            // Set Image
            SetViewer(Index);
        }






    }
}
